package com.hbt.en_rem_hou.service;

import com.hbt.en_rem_hou.dao.FeatureDao;
import com.hbt.en_rem_hou.dao.WordFeaDao;
import com.hbt.en_rem_hou.entitys.Feature;
import com.hbt.en_rem_hou.entitys.WordFea;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FeatureServiceImp implements FeatureService{
    @Autowired
    FeatureDao featureDao;
    @Autowired
    WordFeaDao wordFeaDao;

    @Override
    public boolean deleteFeatureByDescriptAndWordId(String descript, int worid) {
        //通过descript获取featureid
        Feature f = new Feature();
        f.setDescript(descript);
        f = featureDao.getFeature(f);
        //通过featureid和wordid删除w_f中的行
        WordFea wordFea = new WordFea();
        wordFea.setWordid(worid);
        wordFea.setFeatureid(f.getId());
        wordFeaDao.deleteWordFea(wordFea);
        return true;
    }

    @Override
    public boolean addFeatureByDescriptAndWordid(String descript, int wordid) {
        //从数据库中查找feature表是否存在descript的行
        //若有，则封装为Feature， 若没有则插入并封装为Feature，
        int feature_id = 0;
        Feature f = new Feature();
        f.setDescript(descript);
        f = featureDao.getFeature(f);
        if (f!=null){
            feature_id = f.getId();
        }else {
            Feature f1 = new Feature();
            f1.setDescript(descript);
            featureDao.insertFeature(f1);
            feature_id = f1.getId();
        }

        //通过Feature的id和wordid插入到w_f表中
        WordFea wordFea = new WordFea();
        wordFea.setFeatureid(feature_id);
        wordFea.setWordid(wordid);
        wordFeaDao.insertWordFea(wordFea);

        return true;
    }
}
